package com.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.novel.constants.ReviewEnum;
import com.novel.entiy.Novel;
import com.novel.entiy.Review;
import com.novel.entiy.TpUser;
import com.novel.entiy.dto.PageParams;
import com.novel.entiy.dto.ReviewDTO;
import com.novel.entiy.dto.StatisticsDTO;
import com.novel.entiy.event.HotEvent;
import com.novel.entiy.vo.StatisticsVO;
import com.novel.mapper.NovelMapper;
import com.novel.mapper.ReviewMapper;
import com.novel.service.ManagerService;
import com.novel.service.ReviewService;
import com.novel.service.UserService;
import com.novel.utils.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author tanghao
 * @date 2022/4/10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ManagerServiceImpl implements ManagerService {

    private final ReviewMapper reviewMapper;

    private final UserService userService;

    private final NovelMapper novelMapper;

    @Resource
    private Map<String, ReviewService> reviewServiceMap;

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    /**
     * 获取审批列表
     *
     * @param reviewDTO 参数
     * @return 审批列表
     */
    @Override
    public PageResult<Review> getAllReview(ReviewDTO reviewDTO) {
        QueryWrapper<Review> reviewQueryWrapper = new QueryWrapper<Review>().eq(Review.COL_REVIEW_TYPE, reviewDTO.getReviewType())
                .orderByDesc(Review.COL_CREATED_TIME)
                .orderByAsc(Review.COL_REVIEW_RESULT);
        if (ObjectUtils.isNotEmpty(reviewDTO.getReviewResult())) {
            reviewQueryWrapper.eq(Review.COL_REVIEW_RESULT, reviewDTO.getReviewResult());
        }
        Page<Review> reviewPage = reviewMapper.selectPage(new Page<>(reviewDTO.getPageNum(), reviewDTO.getPageSize(), true),
                reviewQueryWrapper);
        return new PageResult<>(reviewPage);

    }

    /**
     * 审批
     *
     * @param reviewDTO 参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void review(ReviewDTO reviewDTO) {
        TpUser userInfo = userService.getUserInfo();
        Review review = reviewMapper.selectById(reviewDTO.getId());
        review.setReviewId(userInfo.getId());
        review.setReviewUser(userInfo.getNickname());
        review.setReviewResult(reviewDTO.getReviewResult());
        reviewMapper.updateById(review);
        ReviewService reviewService = reviewServiceMap.get(ReviewEnum.getMethod(review.getReviewType()));
        if (reviewDTO.getReviewResult().equals(2)) {
            reviewService.agreeReview(reviewDTO);
        }
        if (reviewDTO.getReviewResult().equals(1)) {
            reviewService.rejectReview(reviewDTO);
        }
    }

    /**
     * 数据统计
     *
     * @param statisticsDTO 作者数据统计
     * @return 结果
     */
    @Override
    public PageResult<StatisticsVO> statistics(StatisticsDTO statisticsDTO) {
        TpUser userInfo = userService.getUserInfo();
        if (userInfo.getRole().equals(1)) {
            statisticsDTO.setAuthorId(userInfo.getId());
        }
        Page<StatisticsVO> statisticsVoPage = novelMapper.statisticsPage(new Page<>(statisticsDTO.getPageNum(), statisticsDTO.getPageSize(), true),
                statisticsDTO);
        return new PageResult<>(statisticsVoPage);
    }

    /**
     * 增加小说热度
     *
     * @param novelId 小说id
     * @param hotNums 热度
     */
    @Override
    public void addNovelHotNums(Long novelId, Long hotNums) {
        //为作品增加热度
        applicationEventPublisher.publishEvent(HotEvent.builder().novelId(novelId)
                .hotNums(hotNums).build());
    }

    /**
     * 获取全部小说列表
     *
     * @param pageParams 参数
     */
    @Override
    public PageResult<Novel> getNovelList(PageParams pageParams) {
        return new PageResult<>(novelMapper.selectPage(new Page<>(pageParams.getPageNum(), pageParams.getPageSize(), true),
                new QueryWrapper<Novel>().orderByDesc(Novel.COL_UPDATED_TIME).orderByAsc(Novel.COL_STATUS)));
    }
}
